package com.cyys.common.utils.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil implements java.io.Serializable {

	GregorianCalendar calendar;

	/**
	 * 获取SimpleDateFormat
	 * @param parttern 日期格式
	 * @return SimpleDateFormat对象
	 * @throws RuntimeException 异常：非法日期格式
	 */
	private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
		return new SimpleDateFormat(parttern);
	}

	/**
	 * 获取日期字符串的日期风格。失敗返回null。
	 * @param date 日期字符串
	 * @return 日期风格
	 */
	public static DateStyle getDateStyle(String date) {
		DateStyle dateStyle = null;
		Map<Long, DateStyle> map = new HashMap<Long, DateStyle>();
		List<Long> timestamps = new ArrayList<Long>();
		for (DateStyle style : DateStyle.values()) {
			Date dateTmp = StringToDate(date, style.getValue());
			if (dateTmp != null) {
				timestamps.add(dateTmp.getTime());
				map.put(dateTmp.getTime(), style);
			}
		}
		dateStyle = map.get(getAccurateDate(timestamps).getTime());
		return dateStyle;
	}

	public static String format(Date date,String format){
		return new SimpleDateFormat(format).format(date);
	}
	
	public DateUtil() {
		calendar = new GregorianCalendar();
	}

	public DateUtil(String time, String format) throws java.text.ParseException {
		this(new SimpleDateFormat(format).parse(time));
	}

	public DateUtil(String time, String format, Locale locale)
			throws java.text.ParseException {
		this(new SimpleDateFormat(format, locale).parse(time));
	}

	public DateUtil(long timeInMillis) {
		calendar = new GregorianCalendar();
		calendar.setTimeInMillis(timeInMillis);
	}

	public DateUtil(Date date) {
		calendar = new GregorianCalendar();
		calendar.setTime(date);
	}

	public DateUtil(int year, int month, int day) {
		calendar = new GregorianCalendar(year, month - 1, day, 0, 0, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	public DateUtil(int year, int month, int day, int hour, int minute,
					int sec, int millisec) {
		calendar = new GregorianCalendar(year, month - 1, day, hour, minute,
				sec);
		calendar.set(Calendar.MILLISECOND, millisec);
	}

	public DateUtil(int hour, int minute, int sec, int millisec) {
		calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, sec);
		calendar.set(Calendar.MILLISECOND, millisec);
	}

	public String toString() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar
				.getTime());
	}

	public Date getTime() {
		return calendar.getTime();
	}

	public void setTime(Date date) {
		calendar.setTime(date);
	}

	public java.sql.Date getSqlTime() {
		return new java.sql.Date(calendar.getTimeInMillis());
	}

	public DateUtil copy() {
		return new DateUtil(calendar.getTime());
	}

	public DateUtil addYears(int years) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.YEAR, years);
		return ret;
	}

	public DateUtil addMonths(int months) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.MONTH, months);
		return ret;
	}

	public DateUtil addDays(int days) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.DAY_OF_MONTH, days);
		return ret;
	}

	public DateUtil addHours(int hours) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.HOUR_OF_DAY, hours);
		return ret;
	}

	public DateUtil addMinutes(int minutes) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.MINUTE, minutes);
		return ret;
	}

	public DateUtil addSeconds(int seconds) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.SECOND, seconds);
		return ret;
	}

	public DateUtil addMilliSeconds(int milliseconds) {
		DateUtil ret = copy();
		ret.calendar.add(Calendar.MILLISECOND, milliseconds);
		return ret;
	}

	public long getTimeInMillis() {
		return calendar.getTimeInMillis();
	}

	public void setTimeInMillis(long timeInMillis) {
		calendar.setTimeInMillis(timeInMillis);
	}

	public void set(int year, int month, int day) {
		calendar.set(year, month - 1, day);
	}

	public void set(int hour, int minute, int second, int millisecond) {
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, millisecond);
	}

	public int getDay() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public int getFullDay() {
		return Integer.parseInt(format("yyyyMMdd"));
	}

	public int getFullMonth() {
		return Integer.parseInt(format("yyyyMM"));
	}

	public void setDay(int day) {
		calendar.set(Calendar.DAY_OF_MONTH, day);
	}

	public int getHour() {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public void setHour(int hour) {
		calendar.set(Calendar.HOUR_OF_DAY, hour);
	}

	public int getMilliSecond() {
		return calendar.get(Calendar.MILLISECOND);
	}

	public void setMilliSecond(int milliSecond) {
		calendar.set(Calendar.MILLISECOND, milliSecond);
	}

	public int getMinute() {
		return calendar.get(Calendar.MINUTE);
	}

	public void setMinute(int minute) {
		calendar.set(Calendar.MINUTE, minute);
	}

	public int getMonth() {
		return calendar.get(Calendar.MONTH) + 1;
	}

	public void setMonth(int month) {
		calendar.set(Calendar.MONTH, month - 1);
	}

	public int getSecond() {
		return calendar.get(Calendar.SECOND);
	}

	public void setSecond(int second) {
		calendar.set(Calendar.SECOND, second);
	}

	public int getYear() {
		return calendar.get(Calendar.YEAR);
	}

	public boolean isLeapYear() {
		return isLeapYear(getYear());
	}

	static int[] m = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	/**
	 * 1秒包含的毫秒数
	 */
	static final long SECOND_MILLISECONDS = 1000;
	/**
	 * 1分钟包含的毫秒数
	 */
	static final long MINUTE_MILLISECONDS = SECOND_MILLISECONDS * 60;
	/**
	 * 1小时包含的毫秒数
	 */
	static final long HOUR_MILLISECONDS = MINUTE_MILLISECONDS * 60;
	/**
	 * 1日包含的毫秒数
	 */
	static final long DAY_MILLISECONDS = HOUR_MILLISECONDS * 24;
	/**
	 * 1周包含的毫秒数
	 */
	static final long WEEK_MILLISECONDS = DAY_MILLISECONDS * 7;
	/**
	 * 1月包含的毫秒数
	 */
	static final double MONTH_MILLISECONDS = DAY_MILLISECONDS * 30.4375;
	/**
	 * 1年包含的毫秒数
	 */
	static final double YEAR_MILLISECONDS = DAY_MILLISECONDS * 365.25;

	static public double milliSecondsBetween(long src, long dst) {
		return Math.abs(src - dst);
	}

	static public double secondsBetween(long src, long dst) {
		return milliSecondsBetween(src, dst) / SECOND_MILLISECONDS;
	}

	static public double minutesBetween(long src, long dst) {
		return milliSecondsBetween(src, dst) / MINUTE_MILLISECONDS;
	}

	static public double hoursBetween(long src, long dst) {
		return milliSecondsBetween(src, dst) / HOUR_MILLISECONDS;
	}

	static public double daysBetween(long src, long dst) {
		return milliSecondsBetween(src, dst) / DAY_MILLISECONDS;
	}

	static public double weeksBetween(long src, long dst) {
		return milliSecondsBetween(src, dst) / WEEK_MILLISECONDS;
	}

	static public double monthsBetween(long src, long dst) {
		return (milliSecondsBetween(src, dst) / MONTH_MILLISECONDS);
	}

	static public double yearsBetween(long src, long dst) {
		return (milliSecondsBetween(src, dst) / HOUR_MILLISECONDS);
	}

	static public double milliSecondsBetween(Date src, Date dst) {
		return milliSecondsBetween(src.getTime(), dst.getTime());
	}

	static public double secondsBetween(Date src, Date dst) {
		return secondsBetween(src.getTime(), dst.getTime());
	}

	static public double minutesBetween(Date src, Date dst) {
		return minutesBetween(src.getTime(), dst.getTime());
	}

	static public double hoursBetween(Date src, Date dst) {
		return hoursBetween(src.getTime(), dst.getTime());
	}

	static public double daysBetween(Date src, Date dst) {
		return daysBetween(src.getTime(), dst.getTime());
	}

	static public double weeksBetween(Date src, Date dst) {
		return weeksBetween(src.getTime(), dst.getTime());
	}

	static public double monthsBetween(Date src, Date dst) {
		return monthsBetween(src.getTime(), dst.getTime());
	}

	static public double yearsBetween(Date src, Date dst) {
		return yearsBetween(src.getTime(), dst.getTime());
	}

	public int getDaysInMonth() {
		return getDaysInMonth(getYear(), getMonth());
	}

	public int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public int getDayOfMonth() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public int getDayOfYear() {
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	public int getDaysInYear() {
		if (isLeapYear())
			return 366;
		else
			return 365;
	}

	public void setYear(int year) {
		calendar.set(Calendar.YEAR, year);
	}

	public String format(String format) {
		SimpleDateFormat f = new SimpleDateFormat(format);
		return f.format(this.getTime());
	}

	public String format(String format, Locale locale) {
		SimpleDateFormat f = new SimpleDateFormat(format, locale);
		return f.format(this.getTime());
	}

	public double milliSecondsBetween(DateUtil t) {
		return milliSecondsBetween(this.getTime(), t.getTime());
	}

	public double secondsBetween(DateUtil t) {
		return secondsBetween(this.getTime(), t.getTime());
	}

	public double minutesBetween(DateUtil t) {
		return minutesBetween(this.getTime(), t.getTime());
	}

	public double hoursBetween(DateUtil t) {
		return hoursBetween(this.getTime(), t.getTime());
	}

	public double daysBetween(DateUtil t) {
		return daysBetween(this.getTime(), t.getTime());
	}

	public double monthsBetween(DateUtil t) {
		return monthsBetween(this.getTime(), t.getTime());
	}

	public double yearsBetween(DateUtil t) {
		return yearsBetween(this.getTime(), t.getTime());
	}

	static public boolean isLeapYear(int year) {
		return (year % 4 == 0) && (((year % 100) != 0) || ((year % 400) == 0));
	}

	static public int getDaysInMonth(int year, int month) {
		if (month == 2 && isLeapYear(year))
			return 29;
		return m[month - 1];
	}

	static public int getDaysInYear(int year) {
		if (isLeapYear(year))
			return 365;
		else
			return 366;
	}

	/**
	 * 获取精确的日期
	 * @param timestamps 时间long集合
	 * @return 日期
	 */
	private static Date getAccurateDate(List<Long> timestamps) {
		Date date = null;
		long timestamp = 0;
		Map<Long, long[]> map = new HashMap<Long, long[]>();
		List<Long> absoluteValues = new ArrayList<Long>();

		if (timestamps != null && timestamps.size() > 0) {
			if (timestamps.size() > 1) {
				for (int i = 0; i < timestamps.size(); i++) {
					for (int j = i + 1; j < timestamps.size(); j++) {
						long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
						absoluteValues.add(absoluteValue);
						long[] timestampTmp = { timestamps.get(i), timestamps.get(j) };
						map.put(absoluteValue, timestampTmp);
					}
				}

				// 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的
				long minAbsoluteValue = -1;
				if (!absoluteValues.isEmpty()) {
					// 如果timestamps的size为2，这是差值只有一个，因此要给默认值
					minAbsoluteValue = absoluteValues.get(0);
				}
				for (int i = 0; i < absoluteValues.size(); i++) {
					for (int j = i + 1; j < absoluteValues.size(); j++) {
						if (absoluteValues.get(i) > absoluteValues.get(j)) {
							minAbsoluteValue = absoluteValues.get(j);
						} else {
							minAbsoluteValue = absoluteValues.get(i);
						}
					}
				}

				if (minAbsoluteValue != -1) {
					long[] timestampsLastTmp = map.get(minAbsoluteValue);
					if (absoluteValues.size() > 1) {
						timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
					} else if (absoluteValues.size() == 1) {
						// 当timestamps的size为2，需要与当前时间作为参照
						long dateOne = timestampsLastTmp[0];
						long dateTwo = timestampsLastTmp[1];
						if ((Math.abs(dateOne - dateTwo)) < 100000000000L) {
							timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
						} else {
							long now = new Date().getTime();
							if (Math.abs(dateOne - now) <= Math.abs(dateTwo - now)) {
								timestamp = dateOne;
							} else {
								timestamp = dateTwo;
							}
						}
					}
				}
			} else {
				timestamp = timestamps.get(0);
			}
		}

		if (timestamp != 0) {
			date = new Date(timestamp);
		}
		return date;
	}

	/**
	 * 获取人性化的时间显示
	 * 
	 * @param currentTime
	 * @param prevTime
	 * @return
	 */
	static public String getHumanTimeString(long currentTime, long prevTime) {
		long seconds = (long) secondsBetween(currentTime, prevTime);
		if (seconds < 10) {
			return "刚才";
		} else if (seconds < 60)
			return seconds + "秒前";
		else if (seconds < 3600)
			return (seconds / 60) + "分钟前";
		else {
			DateUtil t1 = new DateUtil(currentTime);
			DateUtil t2 = new DateUtil(prevTime);
			int y1 = t1.getYear(), m1 = t1.getMonth(), d1 = t1.getDay();
			int y2 = t2.getYear(), m2 = t2.getMonth(), d2 = t2.getDay();
			long days = (long) new DateUtil(y1, m1, d1)
					.daysBetween(new DateUtil(y2, m2, d2));
			if (days == 0)
				return (seconds / 3600) + "小时前";
			else {
				if (days <= 1)
					return "昨天" + t2.format("HH:mm");
				else if (days == 2)
					return "前天" + t2.format("HH:mm");
				else {
					if (y1 == y2)
						return m2 + "月" + d2 + "日";
					else if (y1 == y2 + 1)
						return "去年" + m2 + "月" + d2 + "日";
					else if (y1 == y2 + 2)
						return "前年" + m2 + "月" + d2 + "日";
					else
						return y2 + "年" + m2 + "月" + d2 + "日";
				}
			}
		}
	}

	/**
	 * 获取人性化的时间显示
	 * 
	 * @param currentTime
	 * @param prevTime
	 * @return
	 */
	static public String getShortHumanTimeString(long currentTime, long prevTime) {
		long seconds = (long) secondsBetween(currentTime, prevTime);
		if (seconds < 10) {
			return "刚才";
		} else if (seconds < 60)
			return seconds + "秒前";
		else if (seconds < 3600)
			return (seconds / 60) + "分钟前";
		else {
			DateUtil t1 = new DateUtil(currentTime);
			DateUtil t2 = new DateUtil(prevTime);
			int y1 = t1.getYear(), m1 = t1.getMonth(), d1 = t1.getDay();
			int y2 = t2.getYear(), m2 = t2.getMonth(), d2 = t2.getDay();
			long days = (long) new DateUtil(y1, m1, d1)
					.daysBetween(new DateUtil(y2, m2, d2));
			if (days == 0)
				return (seconds / 3600) + "小时前";
			else {
				if (days <= 1)
					return "昨天" + t2.format("HH:mm");
				else if (days == 2)
					return "前天" + t2.format("HH:mm");
				else {
					if (y1 == y2)
						return m2 + "月" + d2 + "日";
					else if (y1 == y2 + 1)
						return "去年" + m2 + "月";
					else if (y1 == y2 + 2)
						return "前年" + m2 + "月";
					else
						return "很久";
				}
			}
		}
	}

	static public String getLeftTimeString(long currentTime, long prevTime) {
		long seconds = (long) secondsBetween(currentTime, prevTime);
		long days = seconds / 86400;
		seconds %= 86400;
		long hours = seconds / 3600;
		seconds %= 3600;
		long minutes = seconds / 60;
		seconds %= 60;
		if (days > 0) {
			if (hours > 0) {
				if (minutes > 0) {
					if (seconds > 0) {
						return days + " 天 " + hours + " 小时 " + minutes + " 分 "
								+ seconds + " 秒";
					} else
						return days + " 天 " + hours + " 小时 " + minutes + " 分 ";
				} else {
					if (seconds > 0) {
						return days + " 天 " + hours + " 小时 零 " + seconds + " 秒";
					} else
						return days + " 天 " + hours + " 小时 ";
				}
			} else {
				if (minutes > 0) {
					if (seconds > 0) {
						return days + " 天  零 " + minutes + " 分 " + seconds
								+ " 秒";
					} else
						return days + " 天  零 " + minutes + " 分 ";
				} else {
					if (seconds > 0) {
						return days + " 天  零 " + seconds + " 秒";
					} else
						return days + " 天 ";
				}
			}
		} else {
			if (hours > 0) {
				if (minutes > 0) {
					if (seconds > 0) {
						return hours + " 小时 " + minutes + " 分 " + seconds
								+ " 秒";
					} else
						return hours + " 小时 " + minutes + " 分 ";
				} else {
					if (seconds > 0) {
						return hours + " 小时 零 " + seconds + " 秒";
					} else
						return hours + " 小时 ";
				}
			} else {
				if (minutes > 0) {
					if (seconds > 0) {
						return minutes + " 分 " + seconds + " 秒";
					} else
						return minutes + " 分 ";
				} else {
					if (seconds > 0) {
						return seconds + " 秒";
					} else
						return "0秒";
				}
			}
		}
	}


	/**
	 * 判断时间是不是今天
	 * @param date
	 * @return    是返回true，不是返回false
	 */
	public static boolean isToday(Date date,Date now) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		//获取今天的日期
		String nowDay = sf.format(now);
		//对比的时间
		String day = sf.format(date);
		return day.equals(nowDay);
	}

	/**
	 * 获取指定日期格式的时间戳
	 * @param time yyyy-MM-dd HH:mm:ss yyyy-MM-dd格式时间字符串
	 * @return 返回 Long
	 * @throws ParseException
	 */
	public static long getCurrentDayLong(String time) throws ParseException {
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = df.parse(time);
			return date.getTime();
		} catch (Exception e) {
			try {
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date date = df.parse(time);
				return date.getTime();
			} catch (Exception e2) {
				return -1;
			}
		}
	}

	/**
	 * 获取当前时间时间字符串 返回以format格式字符串
	 * @param format 时间格式字符串 yyyy-MM-dd HH:mm:ss ｜ yyyy-MM-dd
	 * @return 按format格式返回 当前时间时间字符串
	 */
	public static String getCurrentDay(String format) {
		DateFormat df = new SimpleDateFormat(format);
		Date data=new Date();
		String date = df.format(data);
		return date;
	}

	/**
	 * 将字符串日期向后加指定年
	 * @param time yyyy-MM-dd格式时间字符串
	 * @param year 日期增量
	 * @return 返回 yyyy-MM-dd格式时间字符串
	 * @throws ParseException
	 */
	public static String addParamYear(String time,Integer year) throws ParseException {
		if(year==null){
			year = 1;
		}
		SimpleDateFormat simple = new SimpleDateFormat( "yyyy-MM-dd");
		Date times = simple.parse(time);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(times);
		calendar.add(calendar.YEAR,year);
		times=calendar.getTime();
		time = simple.format(times);
		return time;
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。
	 * @param date 日期字符串
	 * @return 日期
	 */
	public static Date StringToDate(String date) {
		DateStyle dateStyle = null;
		return StringToDate(date, dateStyle);
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。
	 * @param date 日期字符串
	 * @param parttern 日期格式
	 * @return 日期
	 */
	public static Date StringToDate(String date, String parttern) {
		Date myDate = null;
		if (date != null) {
			try {
				myDate = getDateFormat(parttern).parse(date);
			} catch (Exception e) {
			}
		}
		return myDate;
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。
	 * @param date 日期字符串
	 * @param dateStyle 日期风格
	 * @return 日期
	 */
	public static Date StringToDate(String date, DateStyle dateStyle) {
		Date myDate = null;
		if (dateStyle == null) {
			List<Long> timestamps = new ArrayList<Long>();
			for (DateStyle style : DateStyle.values()) {
				Date dateTmp = StringToDate(date, style.getValue());
				if (dateTmp != null) {
					timestamps.add(dateTmp.getTime());
				}
			}
			myDate = getAccurateDate(timestamps);
		} else {
			myDate = StringToDate(date, dateStyle.getValue());
		}
		return myDate;
	}

	/**
	 * 将日期转化为日期字符串。失败返回null。
	 * @param date 日期
	 * @param parttern 日期格式
	 * @return 日期字符串
	 */
	public static String DateToString(Date date, String parttern) {
		String dateString = null;
		if (date != null) {
			try {
				dateString = getDateFormat(parttern).format(date);
			} catch (Exception e) {
			}
		}
		return dateString;
	}

	/**
	 * 将日期转化为日期字符串。失败返回null。
	 * @param date 日期
	 * @param dateStyle 日期风格
	 * @return 日期字符串
	 */
	public static String DateToString(Date date, DateStyle dateStyle) {
		String dateString = null;
		if (dateStyle != null) {
			dateString = DateToString(date, dateStyle.getValue());
		}
		return dateString;
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。
	 * @param date 旧日期字符串
	 * @param parttern 新日期格式
	 * @return 新日期字符串
	 */
	public static String StringToString(String date, String parttern) {
		return StringToString(date, null, parttern);
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。
	 * @param date 旧日期字符串
	 * @param dateStyle 新日期风格
	 * @return 新日期字符串
	 */
	public static String StringToString(String date, DateStyle dateStyle) {
		return StringToString(date, null, dateStyle);
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。
	 * @param date 旧日期字符串
	 * @param olddParttern 旧日期格式
	 * @param newParttern 新日期格式
	 * @return 新日期字符串
	 */
	public static String StringToString(String date, String olddParttern, String newParttern) {
		String dateString = null;
		if (olddParttern == null) {
			DateStyle style = getDateStyle(date);
			if (style != null) {
				Date myDate = StringToDate(date, style.getValue());
				dateString = DateToString(myDate, newParttern);
			}
		} else {
			Date myDate = StringToDate(date, olddParttern);
			dateString = DateToString(myDate, newParttern);
		}
		return dateString;
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。
	 * @param date 旧日期字符串
	 * @param olddDteStyle 旧日期风格
	 * @param newDateStyle 新日期风格
	 * @return 新日期字符串
	 */
	public static String StringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) {
		String dateString = null;
		if (olddDteStyle == null) {
			DateStyle style = getDateStyle(date);
			dateString = StringToString(date, style.getValue(), newDateStyle.getValue());
		} else {
			dateString = StringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
		}
		return dateString;
	}

	/**
	 * 获取日期中的某数值。如获取月份
	 * @param date 日期
	 * @param dateType 日期格式
	 * @return 数值
	 */
	private static int getInteger(Date date, int dateType) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(dateType);
	}

	/**
	 * 获取日期的年份。失败返回0。
	 * @param date 日期字符串
	 * @return 年份
	 */
	public static int getYear(String date) {
		return getYear(StringToDate(date));
	}

	/**
	 * 获取日期的年份。失败返回0。
	 * @param date 日期
	 * @return 年份
	 */
	public static int getYear(Date date) {
		return getInteger(date, Calendar.YEAR);
	}

	/**
	 * 获取日期的月份。失败返回0。
	 * @param date 日期字符串
	 * @return 月份
	 */
	public static int getMonth(String date) {
		return getMonth(StringToDate(date));
	}

	/**
	 * 获取日期的月份。失败返回0。
	 * @param date 日期
	 * @return 月份
	 */
	public static int getMonth(Date date) {
		return getInteger(date, Calendar.MONTH);
	}

	/**
	 * 获取日期的天数。失败返回0。
	 * @param date 日期字符串
	 * @return 天
	 */
	public static int getDay(String date) {
		return getDay(StringToDate(date));
	}

	/**
	 * 获取日期的天数。失败返回0。
	 * @param date 日期
	 * @return 天
	 */
	public static int getDay(Date date) {
		return getInteger(date, Calendar.DATE);
	}

	/**
	 * 获取日期的小时。失败返回0。
	 * @param date 日期字符串
	 * @return 小时
	 */
	public static int getHour(String date) {
		return getHour(StringToDate(date));
	}

	/**
	 * 获取日期的小时。失败返回0。
	 * @param date 日期
	 * @return 小时
	 */
	public static int getHour(Date date) {
		return getInteger(date, Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取日期的分钟。失败返回0。
	 * @param date 日期字符串
	 * @return 分钟
	 */
	public static int getMinute(String date) {
		return getMinute(StringToDate(date));
	}

	/**
	 * 获取日期的分钟。失败返回0。
	 * @param date 日期
	 * @return 分钟
	 */
	public static int getMinute(Date date) {
		return getInteger(date, Calendar.MINUTE);
	}

	/**
	 * 获取日期的秒钟。失败返回0。
	 * @param date 日期字符串
	 * @return 秒钟
	 */
	public static int getSecond(String date) {
		return getSecond(StringToDate(date));
	}

	/**
	 * 获取日期的秒钟。失败返回0。
	 * @param date 日期
	 * @return 秒钟
	 */
	public static int getSecond(Date date) {
		return getInteger(date, Calendar.SECOND);
	}

	/**
	 * 获取日期 。默认yyyy-MM-dd格式。失败返回null。
	 * @param date 日期字符串
	 * @return 日期
	 */
	public static String getDate(String date) {
		return StringToString(date, DateStyle.YYYY_MM_DD);
	}

	/**
	 * 获取日期。默认yyyy-MM-dd格式。失败返回null。
	 * @param date 日期
	 * @return 日期
	 */
	public static String getDate(Date date) {
		return DateToString(date, DateStyle.YYYY_MM_DD);
	}

	/**
	 * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
	 * @param date 日期字符串
	 * @return 时间
	 */
	public static String getTime(String date) {
		return StringToString(date, DateStyle.HH_MM_SS);
	}

	/**
	 * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
	 * @param date 日期
	 * @return 时间
	 */
	public static String getTime(Date date) {
		return DateToString(date, DateStyle.HH_MM_SS);
	}

	/**
	 * 增加日期中某类型的某数值。如增加日期
	 * @param date 日期字符串
	 * @param dateType 类型
	 * @param amount 数值
	 * @return 计算后日期字符串
	 */
	private static String addInteger(String date, int dateType, int amount) {
		String dateString = null;
		DateStyle dateStyle = getDateStyle(date);
		if (dateStyle != null) {
			Date myDate = StringToDate(date, dateStyle);
			myDate = addInteger(myDate, dateType, amount);
			dateString = DateToString(myDate, dateStyle);
		}
		return dateString;
	}

	/**
	 * 增加日期中某类型的某数值。如增加日期
	 * @param date 日期
	 * @param dateType 类型
	 * @param amount 数值
	 * @return 计算后日期
	 */
	private static Date addInteger(Date date, int dateType, int amount) {
		Date myDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(dateType, amount);
			myDate = calendar.getTime();
		}
		return myDate;
	}

	/**
	 * 增加日期的年份。失败返回null。
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加年份后的日期字符串
	 */
	public static String addYear(String date, int yearAmount) {
		return addInteger(date, Calendar.YEAR, yearAmount);
	}

	/**
	 * 增加日期的年份。失败返回null。
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加年份后的日期
	 */
	public static Date addYear(Date date, int yearAmount) {
		return addInteger(date, Calendar.YEAR, yearAmount);
	}

	/**
	 * 增加日期的月份。失败返回null。
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加月份后的日期字符串
	 */
	public static String addMonth(String date, int yearAmount) {
		return addInteger(date, Calendar.MONTH, yearAmount);
	}

	/**
	 * 增加日期的月份。失败返回null。
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加月份后的日期
	 */
	public static Date addMonth(Date date, int yearAmount) {
		return addInteger(date, Calendar.MONTH, yearAmount);
	}

	/**
	 * 增加日期的天数。失败返回null。
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加天数后的日期字符串
	 */
	public static String addDay(String date, int dayAmount) {
		return addInteger(date, Calendar.DATE, dayAmount);
	}

	/**
	 * 增加日期的天数。失败返回null。
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加天数后的日期
	 */
	public static Date addDay(Date date, int dayAmount) {
		return addInteger(date, Calendar.DATE, dayAmount);
	}

	/**
	 * 增加日期的小时。失败返回null。
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加小时后的日期字符串
	 */
	public static String addHour(String date, int hourAmount) {
		return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
	}

	/**
	 * 增加日期的小时。失败返回null。
	 * @param date 日期
	 * @param hourAmount 增加数量。可为负数
	 * @return 增加小时后的日期
	 */
	public static Date addHour(Date date, int hourAmount) {
		return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
	}

	/**
	 * 增加日期的分钟。失败返回null。
	 * @param date 日期字符串
	 * @param hourAmount 增加数量。可为负数
	 * @return 增加分钟后的日期字符串
	 */
	public static String addMinute(String date, int hourAmount) {
		return addInteger(date, Calendar.MINUTE, hourAmount);
	}

	/**
	 * 增加日期的分钟。失败返回null。
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加分钟后的日期
	 */
	public static Date addMinute(Date date, int hourAmount) {
		return addInteger(date, Calendar.MINUTE, hourAmount);
	}

	/**
	 * 增加日期的秒钟。失败返回null。
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加秒钟后的日期字符串
	 */
	public static String addSecond(String date, int hourAmount) {
		return addInteger(date, Calendar.SECOND, hourAmount);
	}

	/**
	 * 增加日期的秒钟。失败返回null。
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加秒钟后的日期
	 */
	public static Date addSecond(Date date, int hourAmount) {
		return addInteger(date, Calendar.SECOND, hourAmount);
	}


	/**
	 * 获取日期的星期。失败返回null。
	 * @param date 日期字符串
	 * @return 星期
	 */
	public static Week getWeek(String date) {
		Week week = null;
		DateStyle dateStyle = getDateStyle(date);
		if (dateStyle != null) {
			Date myDate = StringToDate(date, dateStyle);
			week = getWeek(myDate);
		}
		return week;
	}

	/**
	 * 获取日期的星期。失败返回null。
	 * @param date 日期
	 * @return 星期
	 */
	public static Week getWeek(Date date) {
		Week week = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		switch (weekNumber) {
			case 0:
				week = Week.SUNDAY;
				break;
			case 1:
				week = Week.MONDAY;
				break;
			case 2:
				week = Week.TUESDAY;
				break;
			case 3:
				week = Week.WEDNESDAY;
				break;
			case 4:
				week = Week.THURSDAY;
				break;
			case 5:
				week = Week.FRIDAY;
				break;
			case 6:
				week = Week.SATURDAY;
				break;
		}
		return week;
	}

	public static int getIntervalHours(Date date, Date otherDate) {
		long from = date.getTime();
		long to = otherDate.getTime();
		int hours = (int) ((to - from)/(1000 * 60 * 60));
		return hours;
	}


	/**
	 * 获取本月第一天
	 * @param date 日期
	 * @return 相差天数
	 */
	public static Date getMonthOfFirst(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	/**
	 * 获取本月第一天
	 * @param year 年
	 * @param month 月
	 * @return 相差天数
	 */
	public static String getMonthOfFirst(Integer year, Integer month, Integer day){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day, 0 , 0 ,0);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));

		return DateToString(cal.getTime(), DateStyle.YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 获取本月最后一天
	 * @param date 日期
	 * @return 相差天数
	 */
	public static Date getMonthOfLast(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	/**
	 * 获取本月最后一天
	 * @param year 年
	 * @param month 月
	 * @return 相差天数
	 */
	public static String getMonthOfLast(Integer year, Integer month, Integer day){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day,23,59,59);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateToString(cal.getTime(), DateStyle.YYYY_MM_DD_HH_MM_SS);
	}

	public static boolean isWeekend(Date date) throws ParseException, RuntimeException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
			return true;
		else
			return false;
	}


}
